﻿using mozhi.smarterp.Business.Enums;
using Mozhi.Abp.Domain.BasicData;
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp;
using Volo.Abp.Domain.Entities;

namespace mozhi.smarterp.Business.Inventories
{
    public class InventoryTransaction : AggregateRoot<Guid>
    {
        public DocumentType TradingObjectType { get; private set; }

        public Guid TradingObjectId { get; private set; }

        public Guid TradingObjectItemId { get; private set; }

        public DateTime Date { get; private set; }

        public InventoryFlowType Type { get; private set; }

        public Guid ProductId { get; private set; }

        public Product Product { get; private set; }

        public Guid WarehouseId { get; private set; }

        public Warehouse Warehouse { get; private set; }

        public Guid? LocationId { get; private set; }

        public string BatchNumber { get; private set; }

        public string SerialNumber { get; private set; }

        public DateTime? ProductionDate { get; private set; }

        public DateTime? ExpirationDate {  get; private set; }

        public Guid BasicUnitId { get; private set; }

        public decimal BasicQuantity { get; private set; }

        public Guid InventoryRealTimeId { get; private set; }

        private InventoryTransaction() { }

        public InventoryTransaction(Guid id, InventoryFlowType type, DocumentType tradingObjectType, Guid tradingObjectId, Guid tradingObjectItemId, DateTime date,
            Product product, Warehouse warehouse, Guid? locationId, string batchNumber, string serialNumber, DateTime? productionDate, DateTime? expirationDate, decimal quantity)
        {

            Product = product;
            Warehouse = warehouse;

            Id = id;
            ProductId = Product==null ? default : Product.Id;
            BasicUnitId = Product == null ? default : Product.BasicUnitId;
            WarehouseId = Warehouse==null ? default : Warehouse.Id;
            LocationId = locationId;
            BatchNumber = batchNumber;
            SerialNumber = serialNumber;
            ProductionDate = productionDate;
            ExpirationDate = expirationDate;
            TradingObjectType = tradingObjectType;
            TradingObjectId = tradingObjectId;
            TradingObjectItemId = tradingObjectItemId;
            Date = date;
            Type = type;
            ProductionDate = productionDate;
            BasicQuantity = quantity;

            Validate();
        }

        #region
        private void Validate()
        {
            if (Product == null)
                throw new BusinessException("100000", $"产品不存在");

            Product.Validate();

            if(Warehouse==null)
                throw new BusinessException("100000", $"仓库不存在");

            Warehouse.Validate();
            Warehouse.ValidateLoaction(LocationId);


            Check.NotDefaultOrNull<Guid>(ProductId , nameof(ProductId));
            Check.NotDefaultOrNull<Guid>(WarehouseId, nameof(WarehouseId));
            Check.NotDefaultOrNull<DocumentType>(TradingObjectType, nameof(TradingObjectType));
            Check.NotDefaultOrNull<Guid>(TradingObjectId, nameof(TradingObjectId));
            Check.NotDefaultOrNull<Guid>(TradingObjectItemId, nameof(TradingObjectItemId));
            Check.NotDefaultOrNull<DateTime>(Date, nameof(Date));
            Check.NotDefaultOrNull<InventoryFlowType>(Type, nameof(Type));
            Check.NotDefaultOrNull<Guid>(BasicUnitId, nameof(BasicUnitId));
            Check.NotDefaultOrNull<decimal>(BasicQuantity, nameof(BasicQuantity));
        }
        #endregion
    }
}
